%% Wrapper for closures
classdef GClosure < glib.GBoxed

  methods
    function varargout = subsref(obj, S, noutrequested)
      if nargin < 3
        noutrequested = nargout;
        indirectReturn = false;
      else
        if nargout > 1, error 'Too many outputs for indirect return', end
        indirectReturn = true;
      end
      if length(S) > 1, error 'Too many subscripts for GClosure', end
      switch S.type
        case '.'
          if ~strcmp(S.subs, 'isinvalid'), error 'Unknown property for GClosure', end
          ret = isinvalid(obj);
          if indirectReturn, varargout{1} = { ret };
          else varargout{1} = ret; end
        case '()'
          if noutrequested > 1, error 'Too many outputs for closure invocation', end
          ret = glib.g_closure_matlab_invoke(obj.value, S.subs);
          if indirectReturn, varargout{1} = { ret };
          else varargout{1} = ret;
        otherwise
          error 'Unsupported subscript for GClosure'
      end
    end % function subsref
  
    function val = isinvalid(obj)
      val = glib.g_closure_matlab_isinvalid(obj.value);
    end

    function s = char(obj)
      s = sprintf('[Closure at 0x%lx]', uint64(obj.value));
    end

    %% This is an internal constructor!
    function obj = GClosure(gtype, value, cls)
      obj@glib.GBoxed(gtype, value, cls);
    end
                  
  end  % Public Methods

end  % Classdef
